<?php
/**
 * 
 * @author jing
 * 2014.12.2
 *
 */
class CommonController extends Controller {
	public function __construct() {
		parent::__construct ();
		$this->view->web_host = $this->getRequest ()->hostUrl;
		$this->view->web_app_url = $this->getRequest ()->hostUrl . "/index.php";
		//$this->logger = LoggerManager::getLogger('qiniu_log');
	}
	
	/**
	 * 获取知识点列表
	 *
	 */
	public function getKnowledge() {
		$fatherID = $this->getRequest ()->get ( 'curID' ) ? $this->getRequest ()->get ( 'curID' ) : 0;
		$subID = $this->getRequest ()->get ( 'subID' );
	
		if ($subID){
			$resultList = array ();
			$know = new knowledge ();
			$list = $know->getknowsBySubId ( $subID, $fatherID );
			if ($list) {
				for($i = 0; $i < count ( $list ); $i ++) {
					$resultList [$i] ['knowledgeID'] = "" . $list [$i] ['id'];
					$resultList [$i] ['knowledgeName'] = "" . $list [$i] ['name'];
					$new = $know->getknowsBySubId ( $subID, $list [$i] ['id'] );
					$resultList [$i] ['hasChild'] = $new ? "1" : "0";
				}
				$this->view->setMsg ( "获取知识点成功~" );
				$data ['list'] = $resultList;
				$data ['totalCount'] = "" . count ( $resultList );
				$this->view->setState ( "1" );
			} else {
				$this->view->setMsg ( "没有更多了哟!" );
				$data ['list'] = (object) null;
				$data ['totalCount'] = "0";
				$this->view->setState ( "0" );
			}
		}else {
			$data =  ( object ) null;
			$this->view->setState ( "-1" );
			$this->view->setMsg ( "请输入科目ID~" );
		}
		
		$this->view->setData ( $data );
		$this->view->display ( "json" );
	}
	
	/**
	 * 获取解题技巧列表
	 *
	 */
	public function getskill() {
		$fatherID = $this->getRequest ()->get ( 'curID' ) ? $this->getRequest ()->get ( 'curID' ) : 0;
		$subID = $this->getRequest ()->get ( 'subID' );
	
		if ($subID){
			$resultList = array ();
			$know = new knowledge ();
			$list = $know->getskillBySubId ( $subID, $fatherID );
			if ($list) {
				for($i = 0; $i < count ( $list ); $i ++) {
					$resultList [$i] ['skillID'] = "" . $list [$i] ['id'];
					$resultList [$i] ['skillName'] = "" . $list [$i] ['name'];
					$new = $know->getskillBySubId ( $subID, $list [$i] ['id'] );
					$resultList [$i] ['hasChild'] = $new ? "1" : "0";
				}
				$this->view->setMsg ( "获取解题技巧成功~" );
				$data ['list'] = $resultList;
				$data ['totalCount'] = "" . count ( $resultList );
				$this->view->setState ( "1" );
			} else {
				$this->view->setMsg ( "没有更多了哟!" );
				$data ['list'] = $resultList;
				$data ['totalCount'] = "0";
				$this->view->setState ( "0" );
			}
		}else {
			$data =  ( object ) null;
			$this->view->setState ( "-1" );
			$this->view->setMsg ( "请输入科目ID~" );
		}
		
		$this->view->setData ( $data );
		$this->view->display ( "json" );
	}
	
	/**
	 * 获取题目类型-教师端
	 * @author jing
	 */
	public function getItemType(){
		$subID = $this->getRequest ()->get ( 'subID' );
		//题型
		$item_type = new knowledge();
		$item = $item_type->gettypeBySubId($subID);
		if ($item){
			$data['list'] = $item;
			$data ['totalCount'] = count($item);
			$this->view->setState ( "1" );
			$this->view->setData ( $data );
			$this->view->setMsg ( "获取列表成功！" );
			$this->view->display ( "json" );
		} else {
			$data ['list'] = array ();
			$data ['totalCount'] = "0";
			$this->view->setData ( $data );
			$this->view->setState ( "0" );
			$this->view->setMsg ( "没有更多了哟!" );
			$this->view->display ( "json" );
		}
	}
	/**
	 * 获取科目-教师端
	 * @author jing
	 */
	public function getsub(){
		$userinfo = $this->getData ( 'userinfo' );
		$userID = $userinfo ['id'];
		$item_type = new knowledge();
		$item = $item_type->getSubByTeacher($userID);
		if ($item && count($item)<>0){
			$data['list'] = $item;
			$data ['totalCount'] = count($item);
			$this->view->setState ( "1" );
			$this->view->setData ( $data );
			$this->view->setMsg ( "获取列表成功！" );
			$this->view->display ( "json" );
		} else {
			$data =null;
			$this->view->setData ( $data );
			$this->view->setState ( "0" );
			$this->view->setMsg ( "没有更多了哟!" );
			$this->view->display ( "json" );
		}
	}
	
	/**
	 * 上传
	 */
	public function Upload(){
		$qiniu = $this->getApp()->loadUtilClass("QiniuUtil");
		$qiniu_list = $this->getApp()->loadConf("qiniu");
		$qiniu->set_conf($qiniu_list['accessKey'],$qiniu_list['secretKey'],$qiniu_list['bucket_video']);
		$message = $qiniu->get_token();
		if ($message){
			$this->view->setState ( "1" );
			$this->view->setData ( $message );
			$this->view->setMsg ( "获取成功！" );
			$this->view->display ( "json" );
		} else {
			$this->view->setData ( ( object ) null );
			$this->view->setState ( "0" );
			$this->view->setMsg ( "获取失败" );
			$this->view->display ( "json" );
		}
			
	}
	
	/**
	 * 上传音频转码
	 */
	public function Notify(){
		$logger = LoggerManager::getLogger('qiniu_log');
		$logger->debug(json_encode($_POST));
	}
	
}